package com.edu.exam.entity.curriculum.questionbank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 验证规则实体类
 * 对应数据库表：validation_rules
 *
 * 管理试题属性验证规则，包括规则名称、类型、表达式等。
 * 支持验证规则的创建、查询、更新和状态管理。
 * 提供灵活的规则配置和验证机制，确保试题属性的数据质量。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("验证规则表")
@SQLDelete(sql = "UPDATE validation_rules SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class ValidationRule {

    /**
     * 规则唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "rule_id")
    private Long id;

    /**
     * 规则名称
     */
    @NotBlank(message = "规则名称不能为空")
    @Size(max = 100, message = "规则名称长度不能超过100个字符")
    @Column(name = "rule_name", nullable = false, length = 100, unique = true)
    private String ruleName;

    /**
     * 规则类型
     */
    @NotBlank(message = "规则类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "rule_type", nullable = false, length = 50)
    private RuleType ruleType;

    /**
     * 属性类型
     */
    @NotBlank(message = "属性类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "attribute_type", nullable = false, length = 50)
    private AttributeType attributeType;

    /**
     * 规则表达式
     */
    @NotBlank(message = "规则表达式不能为空")
    @Column(name = "expression", nullable = false, columnDefinition = "TEXT")
    private String expression;

    /**
     * 规则描述
     */
    @Size(max = 500, message = "规则描述长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 错误信息
     */
    @NotBlank(message = "错误信息不能为空")
    @Size(max = 200, message = "错误信息长度不能超过200个字符")
    @Column(name = "error_message", nullable = false, length = 200)
    private String errorMessage;

    /**
     * 规则状态
     */
    @NotNull(message = "规则状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private RuleStatus status = RuleStatus.ACTIVE;

    /**
     * 规则优先级
     */
    @Column(name = "priority")
    @Builder.Default
    private Integer priority = 0;

    /**
     * 是否系统规则
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = false;

    /**
     * 是否必需
     */
    @Column(name = "is_required")
    @Builder.Default
    private Boolean isRequired = false;

    /**
     * 规则参数，JSON格式
     */
    @Column(name = "parameters", columnDefinition = "TEXT")
    private String parameters;

    /**
     * 规则示例
     */
    @Size(max = 1000, message = "规则示例长度不能超过1000个字符")
    @Column(name = "example", columnDefinition = "TEXT")
    private String example;

    /**
     * 规则版本
     */
    @Column(name = "rule_version")
    @Builder.Default
    private String ruleVersion = "1.0";

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 记录最后更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 规则类型枚举
     */
    public enum RuleType {
        FORMAT("FORMAT", "格式验证"),
        RANGE("RANGE", "范围验证"),
        LENGTH("LENGTH", "长度验证"),
        PATTERN("PATTERN", "模式验证"),
        BUSINESS("BUSINESS", "业务验证"),
        CUSTOM("CUSTOM", "自定义验证");

        private final String code;
        private final String description;

        RuleType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static RuleType fromCode(String code) {
            for (RuleType type : values()) {
                if (type.getCode().equals(code)) {
                    return type;
                }
            }
            return CUSTOM;
        }
    }

    /**
     * 属性类型枚举
     */
    public enum AttributeType {
        CATEGORY("CATEGORY", "分类属性"),
        TAG("TAG", "标签属性"),
        DIFFICULTY("DIFFICULTY", "难度属性"),
        CONTENT("CONTENT", "内容属性"),
        SCORE("SCORE", "分数属性"),
        TIME("TIME", "时间属性"),
        TYPE("TYPE", "类型属性");

        private final String code;
        private final String description;

        AttributeType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static AttributeType fromCode(String code) {
            for (AttributeType type : values()) {
                if (type.getCode().equals(code)) {
                    return type;
                }
            }
            return TYPE;
        }
    }

    /**
     * 规则状态枚举
     */
    public enum RuleStatus {
        ACTIVE("ACTIVE", "启用"),
        INACTIVE("INACTIVE", "停用"),
        DEPRECATED("DEPRECATED", "已废弃");

        private final String code;
        private final String description;

        RuleStatus(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static RuleStatus fromCode(String code) {
            for (RuleStatus status : values()) {
                if (status.getCode().equals(code)) {
                    return status;
                }
            }
            return ACTIVE;
        }
    }

  }